/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package eproject4g1.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author khiemtd
 */
public class ActionConfig extends ActionSupport implements Preparable, ServletRequestAware{
    /**
     * Attributes
     */
    protected HttpServletRequest httpRequest;
    protected HttpServletResponse httpResponse;
    protected HttpSession httpSession;
    protected Context context;
    
    

    /**
     * Properties
     * @return 
     */
    public HttpServletRequest getHttpRequest() {
        return httpRequest;
    }

    public void setHttpRequest(HttpServletRequest httpRequest) {
        this.httpRequest = httpRequest;
    }

    public HttpServletResponse getHttpResponse() {
        return httpResponse;
    }

    public void setHttpResponse(HttpServletResponse httpResponse) {
        this.httpResponse = httpResponse;
    }

    public HttpSession getHttpSession() {
        return httpSession;
    }

    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }

    public Context getContext() {
        return context;
    }
    
    
    /**
     * Implement methods
     * @throws Exception 
     */
    public void prepare() throws Exception {
        this.httpRequest = ServletActionContext.getRequest();
        this.httpResponse = ServletActionContext.getResponse();
        this.httpSession = this.httpRequest.getSession();
    }
    
    public void setServletRequest(HttpServletRequest hsr) {
        this.httpRequest = hsr;
    }
    
    
    /**
     * EJB3 methods
     */
    public Object ejb3LookupBean(String lookupAddress) throws NamingException {       
            if(this.getContext()==null)
                this.context = new InitialContext();
            return context.lookup(lookupAddress);        
    }
}
